package org.boot.ssm.wxshop.web.controller;

import org.boot.ssm.wxshop.domain.ReceiveAddress;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.service.ReceiveAddressService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @program: wxshop
 * @description: 收获地址控制器
 * @author: 刘涛
 * @create: 2018-03-20 16:43
 **/
@Controller
@RequestMapping(value = "/receiveAddress")
public class ReceiveAddressController {

    @Autowired
    private ReceiveAddressService receiveAddressService;

    /**
     * 去到前台地址管理页面
     *
     * @return
     */
    @RequestMapping(value = "/frontStage/goAddressListPage")
    public String goAddressListPage(ReceiveAddress receiveAddress, HttpSession session, Model model) {
        User user = (User)session.getAttribute("user");
        receiveAddress.setUserId(user.getId());
        receiveAddress.setIsDelete(1);
        model.addAttribute("address",receiveAddressService.queryParamList(receiveAddress));
        return "frontstage/user/address_list";
    }

    /**
     * 去添加页面
     * @return
     */
    @RequestMapping("/frontStage/goAddAddressPage")
    public String goAddAddressPage(){
        return "frontstage/user/address_edit";
    }

    /**
     * 去修改页面
     * @return
     */
    @RequestMapping("/frontStage/goUpdateAddressPage")
    public String goUpdateAddressPage(@RequestParam(name = "id",required = false) Integer id, Model model){
        model.addAttribute("address",receiveAddressService.queryById(id));
        return "frontstage/user/address_edit";
    }


    /**
     * 添加或修改收货地址
     * @param receiveAddress
     * @param session
     * @return
     */
    @RequestMapping(value = "/frontStage/addOrUpdate")
    @ResponseBody
    public JsonResult addOrUpdate(ReceiveAddress receiveAddress,HttpSession session){
        return receiveAddressService.addOrUpdateReceiveAddress(receiveAddress,(User) session.getAttribute("user"));

    }


    /**
     * 逻辑删除用户收货地址
     * @param receiveAddress
     * @return
     */
    @RequestMapping(value = "/frontStage/deleteAddress")
    @ResponseBody
    public JsonResult deleteAddress(ReceiveAddress receiveAddress){
        return receiveAddressService.deleteAddress(receiveAddress);
    }



}
